<template>
	<view class="modal" v-if="visible" @click="handleOutsideClick">
		<view class="modal-content" @click.stop>
			<view class="modal-header">新增房间</view>
			<view class="modal-body">
				<view class="input-group">
					<view class="input-label">房间名称 :</view>
					<input class="input-field" type="text" v-model="roomName" placeholder="请输入房间名称" />
				</view>
				<view class="input-group">
					<view class="input-label">房间图片 :</view>
					<view class="picker" @click="showImagePicker = true">
						<image v-if="roomPic" :src="roomPicMap[roomPic]" class="room-pic" />
						<view v-else class="input-field2">请选择房间图片</view>
					</view>
				</view>
			</view>
			<view class="modal-footer">
				<button class="btn" @click="handleCancel">取消</button>
				<button class="btn btn-primary" @click="handleConfirm">确定</button>
			</view>
		</view>

		<!-- 图片选择器 -->
		<view class="image-picker-modal" v-if="showImagePicker" @click="handleOutsideClick">
			<view class="image-picker-content" @click.stop>
				<view v-for="(pic, index) in roomPicOptions" :key="index" class="image-option"
					@click="selectImage(pic)">
					<image :src="roomPicMap[pic]" class="image-option-pic" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getRoom,
		delRoom,
		addRoom,
		updateRoom
	} from '@/api/room/room.js';

	export default {
		props: {
			visible: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				roomName: '',
				roomPic: '',
				newRoom: {},
				showImagePicker: false,
				roomPicOptions: ['blue', 'bp', 'green', 'purple', 'red', 'yellow'],
				roomList: '',
				roomPicMap: {
					blue: '../../static/images/room/roomblue.png',
					bp: '../../static/images/room/roombp.png',
					green: '../../static/images/room/roomgreen.png',
					purple: '../../static/images/room/roompurple.png',
					red: '../../static/images/room/roomred.png',
					yellow: '../../static/images/room/roomyellow.png',
					userId: this.$store.state.user.user.userId,
				}
			};
		},
		 watch: {
		    visible(newVal) {
		      if (!newVal) {
		        // 当弹窗关闭时，清空输入内容
		        this.roomName = '';
		        this.roomPic = '';
		      }
		    }
		  },
		methods: {
			handleCancel() {
			this.$emit('cancel');
			 // 重置输入内容
			this.roomName = '';
			this.roomPic = '';	
			},
			handleConfirm() {
				//非空校验
				if (!this.roomName || !this.roomPic) {
					uni.showToast({
						title: '请填写所有必填项',
						icon: 'none'
					});
					return;
				}
				console.log(this.$store.state.user.user.userId)
				this.newRoom.roomName = this.roomName;
				this.newRoom.roomPic = this.roomPic;
				this.newRoom.userId = this.$store.state.user.user.userId;

				this.addRoom(this.newRoom);
				console.log(this.newRoom);
				//this.$emit('confirm', { roomName: this.roomName, roomPic: this.roomPic });
				this.$emit('message', 'done')
			},
			//数据传到后端
			addRoom(newRoom) {
				console.log(this.newRoom)
				addRoom(newRoom).then(() => {
					// this.$nextTick(function() { 
					// 	this.getRoomList()
					// });
					// this.getRoomList(); // 重新获取房间列表
				}).catch(error => {
					console.error('新增房间失败:', error.response ? error.response.data : error);
					uni.showToast({
						title: '新增房间失败，请重试',
						icon: 'none'
					});
				
				});
			},
			selectImage(pic) {
				this.roomPic = pic;
				this.showImagePicker = false;
			},
			handleOutsideClick() {
				this.showImagePicker = false;
			},
			getRoomList() {
				getRoom().then(res => {
					console.log(res.rows);
					this.roomList = res.rows;
					console.log(this.roomList)
				})
			},
		},
		onShow() {
			
			this.getRoomList();
		},
	};
</script>

<style scoped>
	.modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.modal-content {
		background-color: white;
		padding: 20px;
		border-radius: 10px;
		width: 80%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.modal-header {
		font-size: 23px;
		font-weight: bold;
		margin-bottom: 10px;
		text-align: center;
		width: 100%;
		color: #4e6490;
	}

	.input-group {
		margin-bottom: 10px;
		width: 100%;
		margin-top: 18px;
	}

	.input-label {
		margin-bottom: 5px;
		font-size: 18px;
	}

	.input-field {
		width: 100%;
		height: 20%;
		padding: 8px;
		border: 1px solid #ccc;
		border-radius: 5px;
		font-size: 18px;
	}

	.input-field2 {
		width: 100%;
		height: 20%;
		padding: 8px;
		color: #848484;
		border-radius: 5px;
		font-size: 18px;
	}

	.picker {
		padding: 5px;
		border: 1px solid #ccc;
		border-radius: 5px;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.room-pic {
		width: 100px;
		height: 90px;
	}

	.modal-footer {
		display: flex;
		justify-content: space-between;
		width: 100%;
		padding-top: 20px;
	}

	.btn {
		padding: 10px;
		border-radius: 5px;
		border: none;
		color: white;
		background-color: #654bff;
		line-height: 25px;
		text-align: center;
		display: inline-block;
	}

	.btn-primary {
		background-color: #654bff;
	}

	.image-picker-modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-picker-content {
		background-color: white;
		padding: 20px;
		border-radius: 10px;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
	}

	.image-option {
		margin: 5px;
	}

	.image-option-pic {
		width: 60px;
		height: 60px;
	}
</style>